<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use app\common\model\Wdocument;
use app\common\model\Document;
use app\common\model\SearchLog;
class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        if($this->request->isAjax()){
            $page = $this->request->post('page',1);
            $size = $this->request->post('size',10);
            $keywords = $this->request->post('keywords','');
            $index = $this->request->post('index','');
            $sort = $this->request->post('sort','');
            $start = $this->request->post('start','');
            $end =  $this->request->post('end','');
            $where = [];
            $orderBy = [];
            $where['status'] = 'normal';
            $where['is_audit'] = 1;
            if($keywords){
                $searchlog = SearchLog::get(['name'=>$keywords]);
                if(empty($searchlog)){
                    $searchlog = new Searchlog();
                    $searchlog->name = $keywords;
                    $searchlog->nums = 1;
                    
                }else{
                    $searchlog->nums = $searchlog['nums'] + 1;
                }
                $searchlog->save();
                $where['title|content'] = ['like','%'.$keywords.'%'];
            }
           if($index){
                switch ($index) {
                    case 'hot':
                        $orderBy['once_clicks'] = 'desc';
                        break;
                    case 'featured':
                        $where['is_featured'] = 1;
                        break;
                }
            }
            if($sort){
                switch ($sort) {
                    case 'latest':
                        $orderBy['createtime'] = 'desc';
                        break;
                    case 'likes':
                        $orderBy['likes'] = 'desc';
                        break;
                    case 'comments':
                        $orderBy['comments'] = 'desc';
                        break;
                    case 'flags':
                        $orderBy['flags'] = 'desc';
                        break;
                    case 'latest_flags':
                        $orderBy['new_flag'] = 'desc';
                        break;
                    default:
                        $orderBy['createtime'] = 'desc';
                        break;
                }
            }
            if($start && $end){
             $where['createtime'] = ['between',[strtotime($start),strtotime($end." 23:59:59")]];
            }
            $weigh = '';
            foreach ($orderBy as $key => $value) {
                $weigh .= $key.' '.$value.',';
            }
            $weigh = rtrim($weigh,',');
            $list = Db::name('Document')->where($where)->order($weigh)->page($page,$size)->select();
            foreach ($list as $key => &$value) {
                $value['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
                $value['content'] = mb_substr(strip_tags($value['content']),0,200).'...';
                if($value['type'] == 'wd'){
                    $user = Db::name('User')->where('id',$value['user_id'])->field('nickname,id,avatar,group_id')->find();
                    $user['group_image'] = $user['group_id']?Db::name('UserGroup')->where('id','in',$user['group_id'])->select():[];
                    $value['user'] = $user;
                    $value['document'] = Db::name('Wdocument')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] = Db::name('Wdcate')->where('id',$value['document']['wdcate_id'])->find();
                    $value['document']['tags'] = Db::name('WdocumentCategory')->alias('dc')->join('wd_wdcate c', 'dc.wdcate_id = c.id')
                    ->where('dc.wdocument_id', $value['doc_id'])->field('c.name,c.id,c.image')->select();
                    
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wd')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wd')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'wf'){
                    $user = Db::name('User')->where('id',$value['user_id'])->field('nickname,id,avatar,group_id')->find();
                    $user['group_image'] = $user['group_id']?Db::name('UserGroup')->where('id','in',$user['group_id'])->select():[];
                    $value['user'] = $user;
                    $value['document'] = Db::name('Wfdocument')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = Db::name('WfdocumentCategory')->alias('dc')->join('wd_wfcate c', 'dc.wfcate_id = c.id')
                    ->where('dc.wfdocument_id', $value['doc_id'])->field('c.name,c.id,c.image')->select();
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wf')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wf')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'wp'){
                    $user = Db::name('User')->where('id',$value['user_id'])->field('nickname,id,avatar,group_id')->find();
                    $user['group_image'] = $user['group_id']?Db::name('UserGroup')->where('id','in',$user['group_id'])->select():[];
                    $value['user'] = $user;
                    $value['document'] = Db::name('Wpdocument')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wp')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wp')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'wr'){
                    $value['user'] = ['nickname'=>'问道官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wr')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wr')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'week'){
                    $value['user'] = ['nickname'=>'闻钱官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Wqdocument')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','week')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','week')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'day'){
                    $value['user'] = ['nickname'=>'问道官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Wqdocument')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','day')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','day')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'fa'){
                    $value['user'] = ['nickname'=>'问道官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Wqdocument')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','fa')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','fa')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                
            }
            return json(['code'=>200,'data'=>$list,'msg'=>'success']);
        
        }
        $website = Db::name('Category')->where('url','/')->find();
        $this->view->assign('website',$website);
        $nav = Db::name('Category')->where('pid',16)->where('status','normal')->field('id,icon,url,name')->select();
        foreach ($nav as $key => &$value) {
            if($value['id'] == 17){
                $value['postCount'] = Db::name('Document')->where('status','normal')->where('is_shang',1)->count();
            }
            if($value['id'] == 18){
                $value['postCount'] =Db::name('Wqdocument')->where('status','normal')->where('type','week')->count();
            }
            if($value['id'] == 19){
                $value['postCount'] =Db::name('Wqdocument')->where('status','normal')->where('type','days')->count();
            }
            if($value['id'] == 20){
                $value['postCount'] =Db::name('Wqdocument')->where('status','normal')->where('type','fa')->count();
            }
        }
        $this->view->assign('zhinan',Db::name('Category')->where('id',21)->find());
        $this->view->assign('navs',$nav);
        return $this->view->fetch();
    }
    public function wdgroup($wdid=null){
        if($this->request->isAjax()){
            $id = $this->request->post('id',0);
            $page = $this->request->post('page',1);
            $size = $this->request->post('size',10);
            $keywords = $this->request->post('keywords','');
            $index = $this->request->post('index','');
            $sort = $this->request->post('sort','');
            $start = $this->request->post('start','');
            $end =  $this->request->post('end','');
            $where = [];
            $orderBy = [];
            $map['d.status'] = $where['status'] = 'normal';
            
            $map['d.is_audit'] = $where['is_audit'] = 1;
            if($keywords){
                $searchlog = SearchLog::get(['name'=>$keywords]);
                if(empty($searchlog)){
                    $searchlog = new Searchlog();
                    $searchlog->name = $keywords;
                    $searchlog->nums = 1;
                    
                }else{
                    $searchlog->nums = $searchlog['nums'] + 1;
                }
                $searchlog->save();
                $map['d.title|d.content'] = $where['title|content'] = ['like','%'.$keywords.'%'];
            }
           if($index){
                switch ($index) {
                    case 'hot':
                        $orderBy['once_clicks'] = 'desc';
                        break;
                    case 'featured':
                        $map['d.is_featured'] = $where['is_featured'] = 1;
                        break;
                }
            }
            if($sort){
                switch ($sort) {
                    case 'latest':
                        $orderBy['createtime'] = 'desc';
                        break;
                    case 'likes':
                        $orderBy['likes'] = 'desc';
                        break;
                    case 'comments':
                        $orderBy['comments'] = 'desc';
                        break;
                    case 'flags':
                        $orderBy['flags'] = 'desc';
                        break;
                    case 'latest_flags':
                        $orderBy['new_flag'] = 'desc';
                        break;
                    default:
                        $orderBy['createtime'] = 'desc';
                        break;
                }
            }
            if($start && $end){
             $map['d.createtime'] = $where['createtime'] = ['between',[strtotime($start),strtotime($end." 23:59:59")]];
            }
            
            $cate = Db::name('Wdcate')->where('status','normal')->where('id',$id)->find();
            if($cate['pid'] == 0){
                $weigh = '';
                foreach ($orderBy as $key => $value) {
                    $weigh .= $key.' '.$value.',';
                }
                $weigh = rtrim($weigh,',');
                 $where['wdcate_id'] = $id;
                 $list =Db::name('wdocument')->where($where)->order($weigh)->page($page,$size)->select();
            }else{
                $weigh = '';
                foreach ($orderBy as $key => $value) {
                    $weigh .= 'd.'.$key.' '.$value.',';
                }
                $weigh = rtrim($weigh,',');
                $map['c.wdcate_id']  = $id;
                $list = Db::name('Wdocument')->alias('d')->join('wd_wdocument_category c','d.id =c.wdocument_id ')->where($map)
                ->order($weigh)->page($page,$size)->field('d.*')->select();
            }
            foreach ($list as $key => &$value) {
                $value['content'] = mb_substr(strip_tags($value['content']),0,100);
                $value['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
                $user = Db::name('User')->where('id',$value['user_id'])->field('nickname,id,avatar,group_id')->find();
                $user['group_image'] = $user['group_id']?Db::name('UserGroup')->where('id','in',$user['group_id'])->select():[];
                $value['user'] = $user;
                $value['category'] = Db::name('Wdcate')->where('id',$value['wdcate_id'])->find();
                $value['tags'] = Db::name('WdocumentCategory')->alias('dc')->join('wd_wdcate c', 'dc.wdcate_id = c.id')
                ->where('dc.wdocument_id', $value['id'])->field('c.name,c.id,c.image')->select();
                
                $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                ->where('d.type','wd')->where('d.doc_id',$value['id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                ->where('d.type','wd')->where('d.doc_id',$value['id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
            }
            return json(['code'=>200,'data'=>$list,'msg'=>'success']);
        
        }
        $cate = Db::name('Wdcate')->where('status','normal')->where('id',$wdid)->find();
        $total['document'] = $cate['pid'] == 0 ? Db::name('Wdocument')->where('wdcate_id',$cate['id'])->count() : Db::name('Wdocument')->alias('d')->join('wd_wdocument_category c','d.id =c.wdocument_id ')->where('c.wdcate_id',$cate['id'])->count(); 
        $total['user'] = $cate['pid'] == 0 ? Db::name('Wdocument')->where('wdcate_id',$cate['id'])->where('is_featured',1)->count('DISTINCT user_id') : Db::name('Wdocument')->alias('d')->join('wd_wdocument_category c','d.id =c.wdocument_id ')->where('c.wdcate_id',$cate['id'])->where('d.is_featured',1)->count('DISTINCT user_id');
        if($cate['pid'] == 0){
           $adviser = Db::name('Adviser')->where('cate_id',$wdid)->count('id');
        }else{
           $adviser = Db::name('AdviserCate')->where('wdcate_id',$wdid)->count(); 
        }
        $this->view->assign('total',$total);
        $this->view->assign('cate',$cate);
        $this->assign('adviser',$adviser);  
        
        return $this->view->fetch();
    }
    public function detail(){
        if($this->request->isAjax()){
            $id = $this->request->post('id',0);

            $where['id'] = $id;
            $data =Db::name('wdocument')->where($where)->find();
            $data['createtime'] = date('Y-m-d H:i:s',$data['createtime']);
            $user = Db::name('User')->where('id',$data['user_id'])->field('nickname,id,avatar,group_id')->find();
            $user['group_image'] = $user['group_id']?Db::name('UserGroup')->where('id','in',$user['group_id'])->select():[];
            $user['doc'] =Db::name('Document')->where('user_id',$user['id'])->where('is_audit',1)->count('id');
            $user['featured'] =Db::name('Document')->where('user_id',$user['id'])->where('is_featured',1)->count('id');
            $user['attention'] =Db::name('UserAttention')->where('user_id',$user['id'])->count('id');
            $data['user'] = $user;
            $data['category'] = Db::name('Wdcate')->where('id',$data['wdcate_id'])->find();
            $data['tags'] = Db::name('WdocumentCategory')->alias('dc')->join('wd_wdcate c', 'dc.wdcate_id = c.id')
            ->where('dc.wdocument_id', $data['id'])->field('c.name,c.id,c.image')->select();
            
            $data['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
            ->where('d.type','wd')->where('d.doc_id',$data['id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
            $data['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
            ->where('d.type','wd')->where('d.doc_id',$data['id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->select();
            if($this->auth->id && $this->auth->level > 0){

            }else{
                $data['content'] = mb_substr(strip_tags($data['content']),0,200).'...';
            }
            return json(['code'=>200,'data'=>$data,'msg'=>'success']);
        
        }
        $id = $this->request->param('id');
        $this->assign('id',$id);
        return $this->view->fetch();
    }
    public function add(){
        if($this->request->isAjax()){
            $params = $this->request->post('row/a');
            Db::startTrans();
            try {
                $params['user_id'] = (int)$this->auth->id;
                $params['status'] = 'normal';
                $doc = Wdocument::create($params,true);
                $docCate = [];
                foreach ($params['tags'] as $value) {
                    $docCate[] = ['wdocument_id'=>$doc->id,'wdcate_id'=>$value];
                }
                Db::name('WdocumentCategory')->insertAll($docCate);
                $document = $doc->toArray();
                $document['doc_id'] = $document['id'];
                unset($document['id']);
                $document['type'] = 'wd';
                Document::create($document,true);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success();   
        }
        $cate = Db::name('Wdcate')->where('status','normal')->where('pid','>',0)->column('name','id');
        $this->assign('cate',$cate);
        return $this->view->fetch();
    }

    public function about(){
        $info = Db::name('Category')->where('id',4)->find();
        $this->assign('info',$info);
        return $this->view->fetch();
    }
    public function reward(){

        if($this->request->isAjax()){
            $page = $this->request->post('page',1);
            $size = $this->request->post('size',10);
            $keywords = $this->request->post('keywords','');
            $index = $this->request->post('index','');
            $sort = $this->request->post('sort','');
            $start = $this->request->post('start','');
            $end =  $this->request->post('end','');
            $where = [];
            $orderBy = [];
            $where['status'] = 'normal';
            $where['is_shang'] = 1;
            $where['is_audit'] = 1;
            if($keywords){
                $searchlog = SearchLog::get(['name'=>$keywords]);
                if(empty($searchlog)){
                    $searchlog = new Searchlog();
                    $searchlog->name = $keywords;
                    $searchlog->nums = 1;
                    
                }else{
                    $searchlog->nums = $searchlog['nums'] + 1;
                }
                $searchlog->save();
                $where['title|content'] = ['like','%'.$keywords.'%'];
            }
           if($index){
                switch ($index) {
                    case 'hot':
                        $orderBy['once_clicks'] = 'desc';
                        break;
                    case 'featured':
                        $where['is_featured'] = 1;
                        break;
                }
            }
            if($sort){
                switch ($sort) {
                    case 'latest':
                        $orderBy['createtime'] = 'desc';
                        break;
                    case 'likes':
                        $orderBy['likes'] = 'desc';
                        break;
                    case 'comments':
                        $orderBy['comments'] = 'desc';
                        break;
                    case 'flags':
                        $orderBy['flags'] = 'desc';
                        break;
                    case 'latest_flags':
                        $orderBy['new_flag'] = 'desc';
                        break;
                    default:
                        $orderBy['createtime'] = 'desc';
                        break;
                }
            }
            if($start && $end){
             $where['createtime'] = ['between',[strtotime($start),strtotime($end." 23:59:59")]];
            }
            $weigh = '';
            foreach ($orderBy as $key => $value) {
                $weigh .= $key.' '.$value.',';
            }
            $weigh = rtrim($weigh,',');
            $list = Db::name('Document')->where($where)->order($weigh)->page($page,$size)->select();
   
            foreach ($list as $key => &$value) {
                $value['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
                $value['content'] = mb_substr(strip_tags($value['content']),0,200).'...';
                if($value['type'] == 'wd'){
                    $user = Db::name('User')->where('id',$value['user_id'])->field('nickname,id,avatar,group_id')->find();
                $user['group_image'] = $user['group_id']?Db::name('UserGroup')->where('id','in',$user['group_id'])->select():[];
                $value['user'] = $user;
                    $value['document'] = Db::name('Wdocument')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] = Db::name('Wdcate')->where('id',$value['document']['wdcate_id'])->find();
                    $value['document']['tags'] = Db::name('WdocumentCategory')->alias('dc')->join('wd_wdcate c', 'dc.wdcate_id = c.id')
                    ->where('dc.wdocument_id', $value['doc_id'])->field('c.name,c.id,c.image')->select();
                    
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wd')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wd')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'wf'){
                    $user = Db::name('User')->where('id',$value['user_id'])->field('nickname,id,avatar,group_id')->find();
                $user['group_image'] = $user['group_id']?Db::name('UserGroup')->where('id','in',$user['group_id'])->select():[];
                $value['user'] = $user;
                    $value['document'] = Db::name('Wfdocument')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = Db::name('WfdocumentCategory')->alias('dc')->join('wd_wfcate c', 'dc.wfcate_id = c.id')
                    ->where('dc.wfdocument_id', $value['doc_id'])->field('c.name,c.id,c.image')->select();
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wf')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wf')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'wp'){
                    $user = Db::name('User')->where('id',$value['user_id'])->field('nickname,id,avatar,group_id')->find();
                $user['group_image'] = $user['group_id']?Db::name('UserGroup')->where('id','in',$user['group_id'])->select():[];
                $value['user'] = $user;
                    $value['document'] = Db::name('Wpdocument')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wp')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wp')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'wr'){
                    $value['user'] = ['nickname'=>'问道官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wr')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','wr')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'week'){
                    $value['user'] = ['nickname'=>'闻钱官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','week')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','week')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'day'){
                    $value['user'] = ['nickname'=>'问道官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','day')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','day')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                if($value['type'] == 'fa'){
                    $value['user'] = ['nickname'=>'问道官方','id'=>0,'avatar'=>'/assets/img/logo.png','group_image'=>[]];
                    $value['document'] = Db::name('Resource')->where('id',$value['doc_id'])->find();
                    $value['document']['category'] =[];
                    $value['document']['tags'] = [];
                    $value['followlist'] = Db::name('DocumentUserFollow')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','fa')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar')->limit(8)->select();
                    $value['commonslist'] = Db::name('DocumentUserComments')->alias('d')->join('wd_user u','u.id=d.user_id')
                    ->where('d.type','fa')->where('d.doc_id',$value['doc_id'])->field('u.id,u.nickname,u.avatar,d.content,DATE_FORMAT(FROM_UNIXTIME(d.createtime), "%Y-%m-%d %H:%i") AS create_time')->limit(10)->select();
                }
                
            }
            return json(['code'=>200,'data'=>$list,'msg'=>'success']);
        
        }
        $website = Db::name('Category')->where('url','/')->find();
        $this->view->assign('website',$website);
        $nav = Db::name('Category')->where('pid',16)->where('status','normal')->field('id,icon,url,name')->select();
        foreach ($nav as $key => &$value) {
            if($value['id'] == 17){
                $value['postCount'] = Db::name('Document')->where('status','normal')->where('is_shang',1)->count();
            }
            if($value['id'] == 18){
                $value['postCount'] =Db::name('Wqdocument')->where('status','normal')->where('type','week')->count();
            }
            if($value['id'] == 19){
                $value['postCount'] =Db::name('Wqdocument')->where('status','normal')->where('type','days')->count();
            }
            if($value['id'] == 20){
                $value['postCount'] =Db::name('Wqdocument')->where('status','normal')->where('type','fa')->count();
            }
        }
        $this->view->assign('zhinan',Db::name('Category')->where('id',21)->find());
        $this->view->assign('navs',$nav);
        return $this->view->fetch();
    }

    public function adviser($wdid=null){
        $cate = Db::name('Wdcate')->where('status','normal')->where('id',$wdid)->find();
        if($cate['pid'] == 0){
            $adviser = Db::name('Adviser')->where('cate_id',$wdid)->order('start_time desc')->select();
         }else{
            $adviser = Db::name('AdviserCate')->alias('c')->join('wd_adviser a','a.id = c.adviser_id')->where('wdcate_id',$wdid)->order('a.start_time desc')->field('a.*')->select(); 
         }
         foreach ($adviser as $key => $value) {
            $adviser[$key]['start_time'] = date('Y-m-d H:i',$value['start_time']);
            $adviser[$key]['nums'] = Db::name('AdviserOrder')->where('adviser_id',$value['id'])->where('pay_status',1)->count('id');
         }
         if($cate['pid'] == 0){
            $cate['adviser'] = Db::name('Adviser')->where('cate_id',$wdid)->count('id');
         }else{
            $cate['adviser']  = Db::name('AdviserCate')->where('wdcate_id',$wdid)->count(); 
         }
         $this->assign('adviser',$adviser);
         $this->assign('cate',$cate);
         return $this->view->fetch();
    }
}
